package java;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Question17 {
    public static void main(String[] args) {
        List<String> res = letterCombinations("123");
        for (String item : res) {
            System.out.println(item);
        }
    }
    public static List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits == null || digits.length() < 1){
            return res;
        }
        int digitsLength = digits.length();
        int index = 1;
        Character[] resChar = new Character[digitsLength];
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"!@#");
        map.put(2,"abc");
        map.put(3,"def");
        map.put(4,"ghi");
        map.put(5,"jkl");
        map.put(6,"mno");
        map.put(7,"pqrs");
        map.put(8,"tuv");
        map.put(9,"wxyz");
        letterCombinationsDiGui(map,digits,resChar,index,digitsLength,res);
        return res;
    }

    private static void letterCombinationsDiGui(Map<Integer, String> map, String digits
            , Character[] resChar,int index,int digitsLength, List<String> res) {
        if (index > digitsLength){
            StringBuilder stringBuilder = new StringBuilder();
            for (Character item: resChar){
                stringBuilder.append(item);
            }
            res.add(stringBuilder.toString());
            return;
        }
        Integer key = Integer.parseInt(String.valueOf(digits.charAt(index - 1)));
        String characters = map.get(key);
        for (int i = 0;i < characters.length(); i ++){
            resChar[index - 1] = characters.charAt(i);
            letterCombinationsDiGui(map,digits,resChar,index + 1,digitsLength,res);
        }
    }
}
